package com.zhy.generator.codegen.mybatis.javacontroller.elements;
import com.zhy.generator.codegen.api.IntrospectedColumn;
import com.zhy.generator.codegen.dom.java.FullyQualifiedJavaType;
import com.zhy.generator.codegen.dom.java.JavaVisibility;
import com.zhy.generator.codegen.dom.java.Method;
import com.zhy.generator.codegen.dom.java.TopLevelClass;
import com.zhy.generator.codegen.mybatis.javacontroller.JavaControllerMethodConstant;
import com.zhy.generator.codegen.mybatis.javahelper.JavaHelperMethodConstant;

import java.util.List;

import static com.zhy.generator.codegen.util.JavaBeansUtil.getCamelCaseString;
import static com.zhy.generator.codegen.util.JavaBeansUtil.getGetterMethodName;

/**
 * @author 源
 */
public class GetMethodGenerator extends AbstractJavaControllerMethodGenerator {
    @Override
    public void addControllerElements(TopLevelClass tlc) {

        Method listMethod = new Method(getMethodStr());
        listMethod.setVisibility(JavaVisibility.PUBLIC);

        String voType = introspectedTable.getVoType();
        FullyQualifiedJavaType javaVoType = new FullyQualifiedJavaType(voType);

        FullyQualifiedJavaType returnType = getReturnType();
        returnType.addTypeArgument(javaVoType);
        listMethod.setReturnType(returnType);

        tlc.addImportedType(returnType);
        tlc.addImportedType(javaVoType);

        addParameter(tlc, listMethod);

        addMethodAnnotations(tlc, listMethod);

        tlc.addMethod(listMethod);

        FullyQualifiedJavaType javaPOType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());

        List<IntrospectedColumn> primaryKeyColumns = introspectedTable.getPrimaryKeyColumns();
        IntrospectedColumn primaryKeyColumn = primaryKeyColumns.get(0);
        FullyQualifiedJavaType fqjt = primaryKeyColumn.getFullyQualifiedJavaType();
        String getterMethodName = getGetterMethodName(primaryKeyColumn.getJavaProperty(), fqjt);

        String queryResult = getCamelCaseString(javaPOType.getShortName(), false);

        listMethod.addBodyLine(javaPOType.getShortName() + " " + queryResult +" = "+getServiceShortName()+".getByPkid(param."+getterMethodName+"());");

        listMethod.addBodyLine("if (" + queryResult +" == null) {");
        listMethod.addBodyLine("    return ResultUtil.jsonMsg(ResultErrCodeEnum.FAIL.getStatus(), \"记录不存在或已删除\", null);");
        listMethod.addBodyLine("}");

        FullyQualifiedJavaType helperType = new FullyQualifiedJavaType(introspectedTable.getHelperType());
        tlc.addImportedType(helperType);
        listMethod.addBodyLine(javaVoType.getShortName() + " vo = "+helperType.getShortName()+"."+ JavaHelperMethodConstant.PO_TO_VO +"("+ queryResult +");");
        listMethod.addBodyLine("return ResultUtil.jsonMsg(ResultErrCodeEnum.SUCCESS.getStatus(), \"查询成功\", vo);");

        tlc.addImportedType(javaPOType);
    }

    @Override
    public String getMethodStr() {
        return JavaControllerMethodConstant.GET_METHOD;
    }


}
